/**
 * 链表节点
 * @param {number} val
 */
function ListNode(val) {
	this.val = val;
	this.next = null;
}
/**
 * 根据number 生成链表
 * @param {number} number
 * @returns {listNode} listNode
 */

function numberList(number) {
	const arrVal = number.toString().split('').reverse();
	let i = 0;
	let head = null;
	let cur = null;
	do {
		const curNow = new ListNode(arrVal[i]);
		if (cur) {
			cur.next = curNow;
			cur = curNow;
		} else {
			cur = curNow;
			head = cur;
		}
		i += 1;
	} while (i < arrVal.length);
	return head;
}

/**
 * 两个节点相加
 * @param {listNode} l1
 * @param {listNode} l2
 * @returns {listNode}
 */
function addTwoNumbers(l1, l2) {
	let list = null;
	let head = null;
	let cur1 = l1;
	let cur2 = l2;
	let prev = 0;
	do {
		let val1 = 0;
		let val2 = 0;
		if (cur1 === null) {
			val1 = 0;
		} else {
			val1 = parseInt(cur1.val, 10);
			cur1 = cur1.next;
		}
		if (cur2 === null) {
			val2 = 0;
		} else {
			val2 = parseInt(cur2.val, 10);
			cur2 = cur2.next;
		}
		let valNow = val1 + val2 + prev;
		if (valNow > 9) {
			prev = 1;
			valNow -= 10;
		} else {
			prev = 0;
		}
		const curNext = new ListNode(valNow);
		if (list) {
			list.next = curNext;
			list = curNext;
		} else {
			list = curNext;
			head = list;
		}
	} while (cur1 !== null || cur2 !== null);
	return head;
}
// const n1 = numberList(158);
// const n2 = numberList(269);

// const x = addTwoNumbers(n1, n2);

// console.log(x);

function addTwoNumbersN(l1, l2) {
	console.log(typeof l1);
	const val1 = parseInt(l1.reverse().join(''), 10);
	const val2 = parseInt(l2.reverse().join(''), 10);
	return (val1 + val2).toString().split('').reverse();
}
const result = addTwoNumbersN([2, 4, 3], [5, 6, 4]);

console.log(result);
